package com.example.demo.controller;

import com.example.demo.bean.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    private User data;
    @Autowired
    UserService service;

    @RequestMapping("/register")
    @ResponseBody
    public Map register(@RequestBody User user) {
        Map map = new HashMap();
        data = service.register(user);
        if (data != null) {
            map.put("success", true);
            map.put("massage", "注册成功");
            map.put("data", data);
        } else {
            map.put("success", false);
            map.put("massage", "注册失败");
            map.put("data", null);
        }
        return map;
    }

    @RequestMapping("/login")
    @ResponseBody
    public Map login(@RequestBody User user) {
        Map map = new HashMap();
        data = service.login(user);
        if (data != null) {
            map.put("success", true);
            map.put("massage", "登录成功");
            map.put("data", data);
        } else {
            map.put("success", false);
            map.put("massage", "登录失败");
            map.put("data", null);
        }
        return map;
    }
}
